package com.inco.cloud.auth.service;


import com.inco.cloud.common.config.IncoPasswordEncoder;
import com.inco.cloud.common.entity.UserEntity;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component("myUserDetailService")
public class UserDetailService implements UserDetailsService {

    @Autowired
    IncoPasswordEncoder passwordEncoder;
    @Override
    @GlobalTransactional
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_admin"));
        UserEntity user = new UserEntity(username,"123456",grantedAuthorities);
        user.setId("0001");
        user.setAvatar("https://avatars.githubusercontent.com/u/28479614");
        user.setRealname("明哥（"+username+"）");
        user.setSex("1");
        user.setEmail("abc@126.com");
        user.setBirthday("1985年12月29日");
        user.setPhone("123456789");
        System.out.println("----------UserDetailService.loadUserByUsername--------------");
        if (user == null) {
            System.out.println("---------user == null---------");
            throw new UsernameNotFoundException("用户名不存在");
        }
        return user;
    }
}
